// pages/salesmans/salesmanListEdit/salesmanListEdit.js
var app = getApp();
var province = '';
var city = '';
var area = '';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow1: false,
    searchImg: '/images/add1.png',
    pageType: 2, //页面类型1:添加2:编辑3:查看
    statusList: [{
        id: 1,
        label: '正常'
      },
      {
        id: 2,
        label: '注销'
      }
    ], //用户状态
    statusIndex: 0, //用户状态选择
    areaInfo_name: [],
    area_id_Info: [],
    franName: '',
    full_name: '',
    account: '',
    password: '',
    repeat_password: '',
    tel: '',
    id_card: '',
    recharge_account_balance: 0,
    commission: 0,
    recharge_commission: 0,
    // province: '',
    // city: '',
    // area: '',
    address: '',
    salesman_info: [], //销售信息
    s_id: '',
    recharge_total: 0,
    card_number: 0,
    recharge_number: '',
    account_balance: '',
    add_time: ""
  },

  onReady: function () {
    this.cascade = this.selectComponent("#cascade");
  },

  showCascade() {
    this.cascade.selection(this.data.area_id_Info);
  },

  cascadeComplete(e) {
    const detail = e.detail;
    this.setData({
      area_id_Info: detail.ids,
      areaInfo_name: detail.names
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pageType = options.type;

    if (pageType == 1) {
      wx.setNavigationBarTitle({
        title: '添加销售'
      });
      //添加重置省市区
      this.setData({
        areaInfo_name: [],
        area_id_Info: []
      });
    } else if (pageType == 2) {
      this.setData({
        s_id: options.s_id
      });
      this.getSalesman();
      wx.setNavigationBarTitle({
        title: '销售编辑'
      });
    } else if (pageType == 3) {
      this.setData({
        s_id: options.s_id
      });
      this.getSalesman();
      wx.setNavigationBarTitle({
        title: '查看销售'
      });
    }
    this.setData({
      pageType: pageType
    })
  },

  handleInput(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    province = '';
    city = '';
    area = '';
  },



  /**
   * 选择加盟商状态
   */
  statusChange: function (e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },
  //选择地址
  addressChange(e) {
    this.setData({
      areaInfo_name: e.detail.address.areaInfo_name,
      area_id_Info: e.detail.address.area_id_Info,
    });
  },
  //名称
  franName(e) {
    this.setData({
      full_name: e.detail.value
    })
  },
  //账号
  franAccount(e) {
    this.setData({
      account: e.detail.value
    })
  },
  //密码
  franPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  //密码确认
  franPasswordAgain(e) {
    this.setData({
      repeat_password: e.detail.value
    })
  },
  //手机号
  franPhone(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  //身份证号
  franIdCard(e) {
    this.setData({
      id_card: e.detail.value
    })
  },
  //分成比例
  franPercentage(e) {
    this.setData({
      commission: e.detail.value
    })
  },
  //充值卡分成比例
  franRePercentage(e) {
    this.setData({
      recharge_commission: e.detail.value
    })
  },
  //详细地址
  franAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  //充值卡账户
  franMoney1(e) {
    this.setData({
      isShow1: true,
      recharge_number: ''
    })
  },
  //充值卡账户
  franMonerInput1(e) {
    this.setData({
      recharge_account_balance: e.detail.value
    })
  },
  //更改充值账户
  franMonerInput2(e) {
    this.setData({
      recharge_number: e.detail.value
    })
  },
  //是否保存,修改充值账户余额
  btnChange1(e) {
    var that = this;
    that.setData({
      isShow: e.detail.btns.isShow
    });
    if (e.detail.btns.type == 'confirm') {
      var value = that.data.recharge_number;
      app.ajax({
        url: 'Partner/Salesman/rechargeAccountBalance',
        data: {
          id: that.data.s_id,
          number: value
        },
        success: function (res) {
          app.showToast(res.data.message);
          if (res.data.code > 0) {
            that.setData({
              recharge_account_balance: res.data.data
            });
          }
        }
      })
    }
  },
  //是否保存
  btnChange2(e) {
    console.log(e.detail.btns.type);
    var that = this;
    that.setData({
      isShow: e.detail.btns.isShow
    })
    if (e.detail.btns.type == 'confirm') {

    }
  },
  //取消添加
  goBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  //保存添加
  addFran() {
    var that = this;
    if (that.data.full_name == '') {
      app.showToast('请输入姓名');
      return false;
    }
    if (that.data.account == '') {
      app.showToast('请输入账号');
      return false;
    }
    if (that.data.password == '') {
      app.showToast('请输入密码');
      return false;
    }
    if (that.data.repeat_password == '') {
      app.showToast('请输入确认密码');
      return false;
    }
    if (that.data.password != that.data.repeat_password) {
      app.showToast('两次输入密码不一致');
      return false;
    }
    if (that.data.tel == '') {
      app.showToast('请输入手机号码');
      return false;
    }
    // if (that.data.id_card == '') {
    //   app.showToast('请输入身份证号码');
    //   return false;
    // }
    if (that.data.recharge_account_balance == '' && that.data.recharge_account_balance != 0) {
      app.showToast('请输入充值金额');
      return false;
    }
    if (that.data.commission == '' && that.data.commission != 0) {
      app.showToast('请输入分成比例');
      return false;
    }
    if (that.data.recharge_commission == '' && that.data.recharge_commission != 0) {
      app.showToast('请输入充值卡分成比例');
      return false;
    }
 
    app.ajax({
      url: 'Partner/Salesman/addSalesman',
      method: "POST",
      data: {
        full_name: that.data.full_name,
        account: that.data.account,
        password: that.data.password,
        repeat_password: that.data.repeat_password,
        tel: that.data.tel,
        id_card: that.data.id_card,
        recharge_account_balance: that.data.recharge_account_balance,
        commission: that.data.commission,
        recharge_commission: that.data.recharge_commission,
        province: that.data.area_id_Info[0],
        city: that.data.area_id_Info[1],
        area: that.data.area_id_Info[2],
        address: that.data.address,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.message, "success");
          that.navigateBackAndUpdate();
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },

  navigateBackAndUpdate() {

    setTimeout(() => {
      const pages = getCurrentPages();
      const targetPage = pages[pages.length - 2];
      targetPage.reloadData();
      wx.navigateBack();
    }, 2000)
  },


  //保存编辑
  editFran() {
    var that = this;
    if (that.data.full_name == '') {
      app.showToast('请输入名称');
      return false;
    }
    if (that.data.account == '') {
      app.showToast('请输入账号');
      return false;
    }
    if (that.data.password != that.data.repeat_password) {
      app.showToast('两次输入密码不一致');
      return false;
    }
    if (that.data.tel == '') {
      app.showToast('请输入手机号码');
      return false;
    }
    // if (that.data.id_card == '') {
    //   app.showToast('请输入身份证号码');
    //   return false;
    // }
    if (that.data.recharge_account_balance == '' && that.data.recharge_account_balance != 0) {
      app.showToast('请输入充值卡账户金额');
      return false;
    }
    if (that.data.commission == '' && that.data.commission != 0) {
      app.showToast('请输入分成比例');
      return false;
    }
    if (that.data.recharge_commission == '' && that.data.recharge_commission != 0) {
      app.showToast('请输入充值卡分成比例');
      return false;
    }
    if (that.data.area_id_Info.length != 3) {
      app.showToast('请选择地址');
      return false;
    }
    if (that.data.address == '') {
      app.showToast('请输入详细地址');
      return false;
    }
    app.ajax({
      url: 'Partner/Salesman/edieSalesman',
      method: "POST",
      data: {
        id: that.data.s_id,
        full_name: that.data.full_name,
        account: that.data.account,
        password: that.data.password,
        repeat_password: that.data.repeat_password,
        tel: that.data.tel,
        id_card: that.data.id_card,
        recharge_account_balance: that.data.recharge_account_balance,
        commission: that.data.commission,
        recharge_commission: that.data.recharge_commission,
        province: that.data.area_id_Info[0],
        city: that.data.area_id_Info[1],
        area: that.data.area_id_Info[2],
        address: that.data.address,
        state: that.data.statusList[that.data.statusIndex].id
      },
      success: function (res) {

        if (res.data.code == 1000) {
          app.showToast(res.data.message, "success");
          that.navigateBackAndUpdate();
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //获取销售信息
  getSalesman() {
    var that = this;
    app.ajax({
      url: 'Partner/Salesman/getSalesman',
      data: {
        id: that.data.s_id,
      },
      success: function (res) {
        if (res.data.code > 0) {

          let statusIndex = that.data.statusList.findIndex(item => item.id == res.data.data.state);
          that.setData({
            areaInfo_name: [res.data.data.province_name, res.data.data.city_name, res.data.data.area_name],
            area_id_Info: [res.data.data.province, res.data.data.city, res.data.data.area],
            // salesman_info:res.data.data,
            full_name: res.data.data.full_name,
            account: res.data.data.account,
            tel: res.data.data.tel,
            id_card: res.data.data.id_card,
            recharge_account_balance: res.data.data.recharge_account_balance,
            commission: parseFloat(res.data.data.commission) * 100,
            recharge_commission: parseFloat(res.data.data.recharge_commission) * 100,
            // province: res.data.data.province,
            // city: res.data.data.city,
            // area: res.data.data.area,
            address: res.data.data.address,
            recharge_total: res.data.data.recharge_total,
            card_number: res.data.data.card_number,
            account_balance: res.data.data.account_balance,
            add_time: res.data.data.add_time,
            statusIndex
          });
          province = res.data.data.province;
          city = res.data.data.city;
          area = res.data.data.area;
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
})